package com.oocl.euc.core.maintenance.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.euc.core.builder.EmailTemplateBuilder;
import com.oocl.euc.core.common.ITTestBase;
import com.oocl.euc.core.maintenance.model.EmailTemplate;
import com.oocl.euc.core.maintenance.repository.EmailTemplateRepository;
import com.oocl.euc.core.maintenance.service.EmailTemplateService;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

public class EmailTemplateControllerIT extends ITTestBase{

    @Autowired
    EmailTemplateService emailTemplateService;
    @Autowired
    ObjectMapper objectMapper;
    @Autowired
    EmailTemplateBuilder emailTemplateBuilder;
    @Autowired
    EmailTemplateRepository emailTemplateRepository;

    @After
    public void reset(){
        emailTemplateRepository.deleteAll();
    }

    @Test
    public void should_return_modified_email_template_when_update() throws Exception {
        EmailTemplate emailTemplate = emailTemplateBuilder.defaultBuilder()
                .withCC("cc")
                .withContent("test")
                .withSuject("subject")
                .withBCC("bcc")
                .saveToDB();
        setEmailTemplate(emailTemplate);
        String requestJson = objectMapper.writeValueAsString(emailTemplate);
        mockMvc.perform(put("/rest/emailTemplate/update").content(requestJson).contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(status().isOk())
                .andExpect(jsonPath("$.content").value(emailTemplate.getContent()))
                .andExpect(jsonPath("$.emailCC").value(emailTemplate.getEmailCC()))
                .andExpect(jsonPath("$.emailBCC").value(emailTemplate.getEmailBCC()))
                .andExpect(jsonPath("$.sendFrom").value(emailTemplate.getSendFrom()))
                .andExpect(jsonPath("$.sendTo").value(emailTemplate.getSendTo()))
                .andExpect(jsonPath("$.subject").value(emailTemplate.getSubject()));
    }

    private void setEmailTemplate(EmailTemplate emailTemplate) {
        emailTemplate.setSubject("subjectU");
        emailTemplate.setContent("testU");
        emailTemplate.setEmailCC("ccU");
        emailTemplate.setEmailBCC("bccU");
        emailTemplate.setSendFrom("fromU");
        emailTemplate.setSendTo("toU");
    }

    @Test
    public void should_return_email_template_when_given_valid_key() throws Exception {
        EmailTemplate emailTemplate = emailTemplateBuilder.defaultBuilder().saveToDB();
        mockMvc.perform(get("/rest/emailTemplate/findByEmailTempalteKey")
                .param("emailTemplateKey", EmailTemplateBuilder.EMAIL_TEMPALTE_KEY).accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"))
                .andExpect(jsonPath("$.emailTempalteKey").value(EmailTemplateBuilder.EMAIL_TEMPALTE_KEY));
    }

    @Test
    public void should_return_email_template_when_given_unvalid_key() throws Exception {
        EmailTemplate emailTemplate = emailTemplateBuilder.defaultBuilder().create();
        mockMvc.perform(get("/rest/emailTemplate/findByEmailTempalteKey")
                .param("emailTemplateKey", EmailTemplateBuilder.EMAIL_TEMPALTE_KEY).accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isNotFound());
    }



}
